﻿using Edu.Entitys;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Users.EntityFrameworkCore;

namespace Edu.EntityFrameworkCore
{
    [ConnectionStringName("Default")]
    public class EduDbContext : AbpDbContext<EduDbContext>
    {
        public DbSet<AppUser> Users { get; set; }
        public DbSet<AppStudent> Students { get; set; }
        public DbSet<AppTeacher> Teachers { get; set; }
        public DbSet<AppAchievement> Achievements { get; set; }
        public DbSet<AppUserAchievement> UserAchievements { get; set; }
        public DbSet<AppStudyTask> StudyTasks { get; set; }
        public DbSet<AppRating> Ratings { get; set; }

        public DbSet<AppUserRank> AppUserRank { get; set; }

        public EduDbContext(DbContextOptions<EduDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);



            /* Configure the shared tables (with included modules) here */

            builder.Entity<AppUser>(b =>
            {
                b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser

                b.ConfigureFullAudited();
                b.ConfigureExtraProperties();
                b.ConfigureConcurrencyStamp();
                b.ConfigureAbpUser();

                //Moved customization to a method so we can share it with the EduMigrationsDbContext class
                b.ConfigureCustomUserProperties();
            });


            builder.Entity<AppTeacher>(b =>
            {
                b.HasOne(p => p.User)
                 .WithOne()
                 .HasForeignKey<AppTeacher>(p => p.UserId);
            });

            builder.Entity<AppStudent>(b =>
            {
                b.HasOne(p => p.User)
                 .WithOne()
                 .HasForeignKey<AppStudent>(p => p.UserId);
            });



            /* Configure your own tables/entities inside the ConfigureEdu method */

            builder.ConfigureEdu();
        }
    }
}
